package com.rz.htk.warehouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rz.core.exception.GlobalException;
import com.rz.htk.type.WarehouseStatus;
import com.rz.htk.warehouse.entity.Warehouse;
import com.rz.htk.warehouse.mapper.WarehouseMapper;
import com.rz.htk.warehouse.service.IWarehouseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName WarehouseServiceImpl
 * @Description 仓储service实现类
 * @Author maweiyue
 * @Date 2019/4/10 10:37
 */
@Service
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse> implements IWarehouseService {

    @Override
    public IPage<Warehouse> getPage(Warehouse warehouse) {
        QueryWrapper<Warehouse> wrapper = new QueryWrapper();
        if (StringUtils.isNotEmpty(warehouse.getCommodityCode())) {
            wrapper.like("commodity_code", warehouse.getCommodityCode());
        }
        if (StringUtils.isNotEmpty(warehouse.getStatus())) {
            wrapper.like("status", warehouse.getStatus());
        }
        Page<Warehouse> page = new Page<>(warehouse.getPageNo(), warehouse.getPageSize());
        IPage<Warehouse> iPage = page(page, wrapper);
        buildResult(iPage.getRecords());
        return iPage;
    }

    private void buildResult(List<Warehouse> records) {
//        records.forEach(item->{
//
//        });
    }

    @Override
    @Transactional
    public boolean saveWarehouse(Warehouse warehouse){

        if (warehouse.getId() == null) {
            if (this.getByCommodityCode(warehouse.getCommodityCode()) != null) {
                throw new GlobalException("该订单已经入库，不能重复入库。");
            }
            warehouse.setStatus(WarehouseStatus.add.getKey());
            warehouse.setInTime(LocalDateTime.now());
        } else {
            warehouse.setUpdtMngId(warehouse.getCrteMngId());
            warehouse.setOutTime(LocalDateTime.now());
            warehouse.setStatus(WarehouseStatus.out.getKey());
            Warehouse old = this.getById(warehouse.getId());
            if (old != null) {
                // 如果入库和出库不是同一个人，要保持入库人信息不变
                warehouse.setCrteMngId(old.getCrteMngId());
            }
        }
        boolean isOk = this.saveOrUpdate(warehouse);
        if (!isOk) {
            throw new GlobalException("库存信息保存失败！");
        }
        return isOk;
    }

    /**
     * 根据订单编号获取库存信息
     * @param commodityCode
     * @return
     */
    public Warehouse getByCommodityCode(String commodityCode){
        QueryWrapper<Warehouse> wrapper = new QueryWrapper();
        wrapper.eq("commodity_code", commodityCode);
        List<Warehouse> list = list(wrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }
}
